package com.powersys.I18N;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.EnumUtils;

/**
 * 獲取 model實例 和 枚舉的國際化 信息
 * @author qi.lan
 *
 */
public class GetModelAndEnumI18N {

	static String classPath=getCurrentPath();
	//获取當前運行
		public static String getCurrentPath(){
			String path=GetModelAndEnumI18N.class.getResource("/").getFile();
			path=path.substring(path.lastIndexOf(":")-1);
			path=path.substring(0, path.lastIndexOf("/"));
			if (path.lastIndexOf("!")>0) {
				path=path.substring(0,path.lastIndexOf("!"));
			}
			
			try {
				path= URLDecoder.decode(path, "utf-8");
			} catch (UnsupportedEncodingException e) {
			}
			return path.replace("/test-", "/")+"/";
		}
	
	public static Map<String, Object> getMessage(String filePath) throws Exception{
		Map<String, Object> msgs=new HashMap<String, Object>();
		File file=new File(filePath);
		if(file.exists()){
			String temp;
			final BufferedReader in = new BufferedReader(new FileReader(file));// 读取网页全部内容
			while ((temp = in.readLine()) != null) {
				try {
					String[] tmps=temp.split("=");
					msgs.put(tmps[0].trim(), tmps[1].trim());
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
			in.close();
		}
		return msgs;
	}
	
	//遍曆包  輸出 字段
	public static void main(String[] args) {
		try {
			printClass("com.chuangke.model.","/modelmessage/messages_zh_CN.properties");//輸出 model 國際化 keys
			
			printEunm("com.chuangke.core.eunm.", "/coremessage/messages_zh_CN.properties");//輸出core 枚舉國際化 keys
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 输出指定包名下的hibernate
	 * @param pkg
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public static void printEunm(String pkg,String resourcePath) throws Exception {
		System.out.println("#================ "+pkg+" ===================");
		final ArrayList<String> fname=new ArrayList<String>();
		String tempPathString=classPath+(pkg.replace(".", "/"));//Filet.pathMerge(path,pkg.replace(".", "/"));
		System.out.println(new File(tempPathString).listFiles().length);
		new File(tempPathString).listFiles(new FilenameFilter() {
			@Override
			public boolean accept(File dir, String name) {
				if(name.lastIndexOf(".class")>0){
					fname.add(name.replace(".class", ""));
				}
				return false;
			}
		});
		
		Map<String, Object> msgs=getMessage(classPath+"/"+resourcePath);
		String [] clzz=fname.toArray(new String[]{});
		int index=pkg.lastIndexOf(".");
		for (int i = 0; i < clzz.length; i++) {
			String classname=pkg+clzz[i];
			Class c=Class.forName(classname);
			Map<String, Enum> mapsMap= EnumUtils.getEnumMap(c);
			Set<String> keys= mapsMap.keySet();
			for (String string : keys) {
				if(!msgs.containsKey(c.getName()+"."+string)){
					System.out.println(c.getName()+"."+string+"="+string);
				}
				
			}
		}
	}
	
	/**
	 * 输出指定包名下的hibernate
	 * @param pkg
	 * @throws Exception
	 */
	public static void printClass(String pkg,String resourcePath) throws Exception {
		System.out.println("#================ "+pkg+" ===================");
		final ArrayList<String> fname=new ArrayList<String>();
		String tempPathString=Filet.pathMerge(classPath,pkg.replace(".", "/"));
		new File(tempPathString).listFiles(new FilenameFilter() {
			@Override
			public boolean accept(File dir, String name) {
				if(name.lastIndexOf(".class")>0){
					fname.add(name.replace(".class", ""));
				}
				return false;
			}
		});
		Map<String, Object> msgs=getMessage(classPath+"/"+resourcePath);
		String [] clzz=fname.toArray(new String[]{});
		int index=pkg.lastIndexOf(".");
		for (int i = 0; i < clzz.length; i++) {
			String classname=pkg+clzz[i];
			Class c=Class.forName(classname);
			Field[] fs=c.getDeclaredFields();
			for (int j = 0; j < fs.length; j++) {
				String fieldName=fs[j].getName();
				if(!msgs.containsKey(classname+"."+fieldName)){
					System.out.println(classname+"."+fieldName+"="+fieldName);
				}
			}
		}
	}
}
